//
//  MBProgressHUD+ZNG.m
//  开店通
//
//  Created by 张浩 on 15/7/29.
//  Copyright (c) 2015年 张浩. All rights reserved.
//

#import "MBProgressHUD+ZNG.h"

@implementation MBProgressHUD (ZNG)

+ (UIView *)currentView
{
    UIViewController * rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UIView *view;

    if ([rootVC isKindOfClass:[UITabBarController class]]   ) {
        UITabBarController *tabBarVC = (UITabBarController *)rootVC;
        UINavigationController *nav = (UINavigationController *)tabBarVC.selectedViewController;
        UIViewController *vc = nav.topViewController;
        view = vc.view;
    }
    else if ([rootVC isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nav = (UINavigationController *)rootVC;
        view = nav.topViewController.view;
    } else {
        view = nil;
    }

    return view;
}

+ (void)showTextMessage:(NSString *)message
{
    return [self showTextMessage:message toView:[self currentView] hideAfter:1.0];
}

+ (void)showTextMessage:(NSString *)message hideAfter:(NSUInteger)after
{
    return [self showTextMessage:message toView:[self currentView] hideAfter:after];
}

+ (void)showTextMessage:(NSString *)message toView:(UIView *)view
{
    return [self showTextMessage:message toView:view hideAfter:1.0];
}

+ (void)showTextMessage:(NSString *)message toView:(UIView *)view hideAfter:(NSUInteger)after
{
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabelText = message;
    hud.detailsLabelFont = [UIFont systemFontOfSize:14.0];

    hud.margin = 15;
    hud.dimBackground = NO;

    hud.mode = MBProgressHUDModeText;
    
    hud.removeFromSuperViewOnHide = YES;
    
    hud.dimBackground = NO;
    
    [hud hide:YES afterDelay:after];
}

+ (void)showLoadingMessage
{
    [self showLoadingMessage:@"火速处理中..."];
}

+ (void)showLoadingMessage:(NSString *)message
{
    [self showLoadingMessage:message toView:[self currentView]];
}

+ (void)showLoadingMessage:(NSString *)message toView:(UIView *)view
{
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabelText = message;
    hud.detailsLabelFont = [UIFont systemFontOfSize:14.0];
    hud.detailsLabelColor = [UIColor blackColor];
    hud.margin = 15;
    hud.customView = [[UIView alloc] init];
    hud.activityIndicatorColor = [UIColor colorWithRed:1.0 green:74/255.0 blue:86/255.0 alpha:1.0];
    hud.dimBackground = NO;
    hud.color = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.8];
    hud.removeFromSuperViewOnHide = YES;
}

+ (void)hideHUD
{
    [self hideHUDForView:[self currentView]];
}

+ (void)hideHUDForView:(UIView *)view
{
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    [self hideHUDForView:view animated:YES];
}
@end
